
package com.eyedog.uilibrary.base;

import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.eyedog.library.base.BaseThreadHandlerActivity;
import com.eyedog.library.utils.StatusBarUtils;
import com.eyedog.uilibrary.R;

public abstract class BaseActivity extends BaseThreadHandlerActivity {
    public boolean immersion = true;// 是否启用沉浸式

    public int statusBarColor = Color.BLACK;// 状态栏颜色

    public int navigationBarColor = Color.BLACK;// 底部导航栏颜色

    protected FrameLayout mRootLayout;

    protected ViewStub mContentLayout;

    protected ImageView mLoadingIV;

    protected AnimationDrawable mLoadingDrawable;

    protected int mStatusBarHeight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mRootLayout = (FrameLayout) findViewById(R.id.ll_root);
        mStatusBarHeight = StatusBarUtils.getStatusBarHeight(this);
        mRootLayout.setPadding(0, mStatusBarHeight, 0, 0);
        mContentLayout = (ViewStub) findViewById(R.id.vs_content);
        mLoadingIV = (ImageView) findViewById(R.id.iv_loading);
        mLoadingDrawable = (AnimationDrawable) getDrawable(R.drawable.sp_loading);
        mLoadingIV.setImageDrawable(mLoadingDrawable);
        mLoadingDrawable.selectDrawable(0);
        mLoadingDrawable.stop();

        onContentCreated(mContentLayout);
    }

    protected abstract void onContentCreated(ViewStub contentLayout);

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        getWindow().setStatusBarColor(statusBarColor);
        getWindow().setNavigationBarColor(navigationBarColor);
        if (immersion) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            flag |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            getWindow().getDecorView().setSystemUiVisibility(flag);

        }
    }

    protected void startLoading() {
        if (mLoadingDrawable != null) {
            mLoadingIV.setVisibility(View.VISIBLE);
            mLoadingDrawable.selectDrawable(0);
            mLoadingDrawable.start();
        }
    }

    protected void stopLoading() {
        if (mLoadingDrawable != null) {
            mLoadingDrawable.stop();
            mLoadingIV.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoadingDrawable != null) {
            mLoadingDrawable.stop();
        }
    }
}
